import {Component, Inject, ViewEncapsulation} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';

// 接口
interface Permit {
    id: string;
    name: string;
    routing: string;
    method: string;
}

@Component({
    selector: 'permissions-permit-form-dialog',
    templateUrl: './permit-form.component.html',
    styleUrls: ['./permit-form.component.scss'],
    encapsulation: ViewEncapsulation.None
})

export class PermitFormDialogComponent {
    action: string;

    permit: Permit = {
        id: '',
        name: '',
        routing: '',
        method: '',
    };

    permitForm: FormGroup;
    dialogTitle: string;

    /**
     * Constructor
     *
     * @param {MatDialogRef<PermitFormDialogComponent>} matDialogRef
     * @param _data
     * @param {FormBuilder} _formBuilder
     */
    constructor(
        public matDialogRef: MatDialogRef<PermitFormDialogComponent>,
        @Inject(MAT_DIALOG_DATA) private _data: any,
        private _formBuilder: FormBuilder
    ) {
        // Set the defaults
        this.action = _data.action;

        if (this.action === 'edit') {
            this.dialogTitle = '编辑权限';
            _data.permit['method'] = _data.permit['method'].toUpperCase();
            this.permit = _data.permit;
        } else {
            this.dialogTitle = '新增权限';
            // this.permit = this.permit;
        }

        this.permitForm = this.createPermitForm();
    }

    // -----------------------------------------------------------------------------------------------------
    // @ Public methods
    // -----------------------------------------------------------------------------------------------------

    /**
     * Create contact form
     *
     * @returns {FormGroup}
     */
    createPermitForm(): FormGroup {
        return this._formBuilder.group({
            id: [this.permit['id']],
            name: [this.permit['name']],
            routing: [this.permit['routing']],
            method: [this.permit['method']],
        });
    }
}
